package com.zl.zsk.pojo;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.ToString;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import javax.persistence.*;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author lxb
 * @version 1.0.0
 * @desc: 用户表
 * @date 2020-03-12 15:43
 */
@Entity
@Table(name = "sys_user")
@ToString
public class SysUser implements UserDetails {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "user_id")
    public Long userId;//用户id
    @Column(name = "company_id")
    private Long companyId;
    @Column(name = "dept_id")
    private Long deptId;//部门id
    @Column(name = "post_id")
    private Long postId;//职位id
    @Column(name = "login_name",length = 30)
    private String loginName;//登录名
    @Column(name = "nick_name",length = 30)
    private String nickName;//昵称
    @Column(name = "real_name",length = 30)
    private String realName;//真实姓名
    @Column(name = "password",length = 100)
    private String password;//登录密码
    @Column(name = "user_type",length = 1)
    private Integer userType;//用户类型（冗余）
    @Column(name = "emp_num")
    private Integer empNum;//工号
    private int sex;//性别
    @Column(name = "email",length = 30)
    private String email;//邮箱
    @Column(length = 15)
    private String mobile;//手机号
    @Column(length = 200)
    private String avatar;//头像地址
    @Column(name = "qq",length = 20)
    private String qq;//qq号
    @Column(name = "user_sign",length = 50)
    private String userSign;//用户签名
    @Column(name = "non_del")
    private boolean nonDel;
    private Integer status;//状态
    @Column(name = "login_ip",length = 20)
    private String loginIP;//最后登录ip
    @Column(name = "login_date")
    private Date loginDate;//最后登录时间
    @Column(name = "create_by")
    private Long createBy;//创建人
    @Column(name = "create_time")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone="GMT+8")
    private Date createTime;//创建时间
    @Column(name = "remark",length = 50)
    private String remark;//备注

    @Transient
    private List<SysMenu> menus;

    @JsonIgnore
    @Transient
    private String username;
    @JsonIgnore
    @Transient
    private boolean accountNonExpired = true;
    @JsonIgnore
    @Transient
    private boolean accountNonLocked = true;
    @JsonIgnore
    @Transient
    private boolean credentialsNonExpired = true;
    @JsonIgnore
    @Transient
    private boolean enabled = true;
    @Transient
    @JsonIgnore
    private List<SimpleGrantedAuthority> authorities;

    public void setUsername(String username) {
        this.username = username;
    }

    public void setAuthorities(List<SimpleGrantedAuthority> authorities) {
        this.authorities = authorities;
    }

    public void setAccountNonExpired(boolean accountNonExpired) {
        this.accountNonExpired = accountNonExpired;
    }

    public void setAccountNonLocked(boolean accountNonLocked) {
        this.accountNonLocked = accountNonLocked;
    }

    public void setCredentialsNonExpired(boolean credentialsNonExpired) {
        this.credentialsNonExpired = credentialsNonExpired;
    }

    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        List<SimpleGrantedAuthority> list = menus.stream().map(menu -> new SimpleGrantedAuthority(menu.getMenuTag())).collect(Collectors.toList());
        return list;
    }

    @Override
    public String getPassword() {
        return password;
    }

    @Override
    public String getUsername() {
        return loginName;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }

    public Long getUserId() {
        return userId;
    }

    public void setUserId(Long userId) {
        this.userId = userId;
    }

    public Long getCompanyId() {
        return companyId;
    }

    public void setCompanyId(Long companyId) {
        this.companyId = companyId;
    }

    public Long getDeptId() {
        return deptId;
    }

    public void setDeptId(Long deptId) {
        this.deptId = deptId;
    }

    public Long getPostId() {
        return postId;
    }

    public void setPostId(Long postId) {
        this.postId = postId;
    }

    public String getLoginName() {
        return loginName;
    }

    public void setLoginName(String loginName) {
        this.loginName = loginName;
    }

    public String getNickName() {
        return nickName;
    }

    public void setNickName(String nickName) {
        this.nickName = nickName;
    }

    public String getRealName() {
        return realName;
    }

    public void setRealName(String realName) {
        this.realName = realName;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Integer getUserType() {
        return userType;
    }

    public void setUserType(Integer userType) {
        this.userType = userType;
    }

    public Integer getEmpNum() {
        return empNum;
    }

    public void setEmpNum(Integer empNum) {
        this.empNum = empNum;
    }

    public int getSex() {
        return sex;
    }

    public void setSex(int sex) {
        this.sex = sex;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getMobile() {
        return mobile;
    }

    public void setMobile(String mobile) {
        this.mobile = mobile;
    }

    public String getAvatar() {
        return avatar;
    }

    public void setAvatar(String avatar) {
        this.avatar = avatar;
    }

    public String getQq() {
        return qq;
    }

    public void setQq(String qq) {
        this.qq = qq;
    }

    public String getUserSign() {
        return userSign;
    }

    public void setUserSign(String userSign) {
        this.userSign = userSign;
    }

    public boolean isNonDel() {
        return nonDel;
    }

    public void setNonDel(boolean nonDel) {
        this.nonDel = nonDel;
    }

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    public String getLoginIP() {
        return loginIP;
    }

    public void setLoginIP(String loginIP) {
        this.loginIP = loginIP;
    }

    public Date getLoginDate() {
        return loginDate;
    }

    public void setLoginDate(Date loginDate) {
        this.loginDate = loginDate;
    }

    public Long getCreateBy() {
        return createBy;
    }

    public void setCreateBy(Long createBy) {
        this.createBy = createBy;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }

    public List<SysMenu> getMenus() {
        return menus;
    }

    public void setMenus(List<SysMenu> menus) {
        this.menus = menus;
    }



    public enum AccoutStatus{
        NORMAL(1,"正常"),LOCKED(2,"锁定"),DELETED(3,"删除");
        public int status;
        public String desc;

        AccoutStatus(int status, String desc) {
            this.status = status;
            this.desc = desc;
        }

        public int getStatus() {
            return status;
        }

        public void setStatus(int status) {
            this.status = status;
        }

        public String getDesc() {
            return desc;
        }

        public void setDesc(String desc) {
            this.desc = desc;
        }
    }
}
